from django.db import models
from users.models import Users, Addr


# 商品类别
class Category(models.Model):
    cate_name = models.CharField(max_length=20,verbose_name='类别名字')
    class Meta:
        db_table='category'
        verbose_name_plural="商品类别"
    def __str__(self):
        return self.cate_name

## 商品表:
class Goods(models.Model):
    sku_name = models.CharField(max_length=100,verbose_name='商品名称')
    price = models.DecimalField(max_digits=13,decimal_places=2,verbose_name='商品价格')
    selling_price = models.DecimalField(max_digits=13,decimal_places=2,verbose_name='当前售价')
    img = models.CharField(max_length=200,verbose_name='商品图片')
    title = models.CharField(max_length=30,verbose_name='商品标题',null=True)
    instruction = models.TextField(verbose_name='商品描述')
    count = models.IntegerField(verbose_name="商品销售数量", default=0)
    stock = models.IntegerField(verbose_name="商品库存数量", default=0)
    cate = models.ForeignKey(Category, on_delete=models.CASCADE, verbose_name='商品类别')
    online = models.BooleanField("是否在售", default=True)

    class Meta:
        db_table = 'goods'
        verbose_name_plural = "商品信息"

    def __str__(self):
        return self.sku_name

## 商品图片表
class GoodImg(models.Model):
    img = models.CharField(max_length=200,verbose_name='图片')
    title = models.CharField(max_length=20, verbose_name="图片的描述", null=True)
    good = models.ForeignKey(Goods, on_delete=models.CASCADE, verbose_name="商品所属图片")

    class Meta:
        db_table = "goodimg"

    def __str__(self):
        return self.title

#轮播图
class Carousel(models.Model):
    imgPath = models.CharField("图片地址", max_length=100, null=False)
    describes = models.CharField("图片描述", max_length=100, null=False)

    class Meta:
        db_table = "carousel"
        verbose_name = "轮播图"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.describes

# 收藏商品
class GoodLike(models.Model):
    user = models.ForeignKey(Users,on_delete=models.CASCADE,verbose_name='用户')
    goods = models.ForeignKey(Goods,on_delete=models.CASCADE,verbose_name='商品')
    class Meta:
        db_table = 'goods_collect'
        verbose_name = '商品收藏'
        verbose_name_plural = verbose_name
    def __str__(self):
        return '%s:%s' % (self.user.username, self.goods.sku_name)

# 订单
class Order(models.Model):
    order_id = models.CharField('订单号',primary_key=True,max_length=64)  # 不自增
    user = models.ForeignKey(Users,on_delete=models.CASCADE,verbose_name='用户')
    address = models.ForeignKey(Addr,on_delete=models.CASCADE,verbose_name='收获地址')
    total_amount = models.DecimalField('总价格',decimal_places=2,max_digits=12)
    total_count = models.IntegerField('总商品数')
    freight = models.DecimalField('运费',max_digits=7,decimal_places=2)
    pay_method = models.IntegerField('支付方式',choices=((1,'支付宝'),(2,'银联')))
    pay_status = models.IntegerField('支付状态',choices=((1,'待支付'),(2,'代发货'),(3,'待收货'),(4,'待评价'),(5,'已完成')))
    create_time = models.DateTimeField('创建时间',auto_now_add=True)
    update_time = models.DateTimeField('修改时间',auto_now=True)
    class Meta:
        verbose_name = '订单'
        verbose_name_plural = verbose_name
    def __str__(self):
        return self.order_id

# 订单商品表
class OrderGoods(models.Model):
    good = models.ForeignKey(Goods,on_delete=models.CASCADE,verbose_name='商品')
    count = models.IntegerField(verbose_name='购买数量')
    price = models.DecimalField(max_digits=12,decimal_places=2,verbose_name='购买时价格')
    comment = models.TextField(verbose_name='评价')
    score = models.SmallIntegerField('评分',choices=((0,'0星'),(1,'1星'),(2,'2星'),(3,'3星'),(4,'4星'),(5,'5星')))
    order = models.ForeignKey(Order,on_delete=models.CASCADE,verbose_name='订单')
    is_anonymous = models.BooleanField('是否匿名评价',default=False)
    is_commented = models.BooleanField('是否评价',default=False)









